//
//  YLMyBankCardListViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/11/7.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMyBankCardListViewController.h"
#import "YLBankCardListTableViewCell.h"
#import "YLReplaceBankCardBeCarefulFooterView.h"
#import "YLChangeBankCardController.h"
#import "YLMineViewModel.h"
#import "WHProgressHUD.h"
#import "YLDisplayAmountDescriptionView.h"
@interface YLMyBankCardListViewController ()
@property (nonatomic, strong) YLMineViewModel *viewModel;
@end

@implementation YLMyBankCardListViewController

- (YLMineViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLMineViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的银行卡";
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLBankCardListTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLBankCardListTableViewCell cellID]];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"changeBankCard"] style:UIBarButtonItemStylePlain target:self action:@selector(changeBankCard)];
    
    __weak typeof(self) weakSelf = self;
    [WHProgressHUD showStatus:@"正在加载我的银行卡"];
    [[self.viewModel.myInfoCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        [weakSelf.tableView reloadData];
    }];
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)changeBankCard
{
    if (!self.viewModel.myInfoModel) {
        return;
    }
    YLChangeBankCardController *changeCardCon = [[YLChangeBankCardController alloc] init];
    changeCardCon.model = self.viewModel.myInfoModel;
    [self.navigationController pushViewController:changeCardCon animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (instancetype)init
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.viewModel.myInfoModel) {
        return 1;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLBankCardListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLBankCardListTableViewCell cellID]];
    cell.model = self.viewModel.myInfoModel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    YLReplaceBankCardBeCarefulFooterView *footerView = [[YLReplaceBankCardBeCarefulFooterView alloc] init];
    __weak typeof(self) weakSelf = self;
    footerView.clickBlock = ^{
        YLDisplayAmountDescriptionView *dis = [[YLDisplayAmountDescriptionView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        dis.type = YLDisplayAmountDescriptionViewTypeBankCard;
        [weakSelf.tabBarController.view addSubview:dis];
        
        
    };
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 4;
}


@end
